#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../src/thread.h"

#define forever for(;;)

static void * threadfunc1(void * arg){
  int *t = NULL;
  printf("tid b %d", *t);
  puts("this message should not be printed!!!");
  thread_exit(arg);
}

static void * threadfunc2(void * arg){
  printf("je suis le thread %d\n",thread_self());
  thread_yield();
  printf("je suis encore le thread %d\n",thread_self());
  thread_exit(arg);
}

int main(){
  thread_t thread1, thread2,t;
  int err;
  void *retval1, *retval2,*retval3;

  err = thread_create(&thread1, threadfunc1, "thread1");
  assert(!err);  thread_yield();
  
  err = thread_create(&thread2, threadfunc2, "thread2");
  assert(!err);
  err = thread_create(&t, threadfunc2, "thread3");
  assert(!err);
  err = thread_join(thread2, &retval2);
  assert(!err);
  err = thread_join(thread1, &retval1);
  assert(!err);
  err = thread_join(t, &retval3);
  assert(!err);
  puts("Bye bye...");
  assert(alarmIsEnabled());
  return EXIT_SUCCESS;
}
